package com.surfilter.massdata.spark.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.surfilter.massdata.spark.model.DnsStatConsts;
import com.surfilter.massdata.spark.model.StructDNS;

/**
 * @author lhfcws
 * @time 14-2-16.
 */
public class DNSParser {

	private static final Logger LOG = LoggerFactory.getLogger(DNSParser.class);

	public static StructDNS parseDNSRecord(String provinceCode, String text) {
		if(provinceCode.equals(DnsStatConsts.JS_PROVINCE)){
			return parseJsDNSRecord(text);
		}else if(provinceCode.equals(DnsStatConsts.ZJ_PROVINCE)){
			return parseZjDNSRecord(text);
		}
		return parseZjDNSRecord(text);
	}

	/**
	 * 浙江DNS解析方法
	 * 
	 * @param text
	 * @return
	 */
	public static StructDNS parseZjDNSRecord(String text) {
		String[] tmp = text.split("\t");
		if (tmp.length == 5) {
			try {
				StructDNS structDNS = new StructDNS();
				
				//这里注意，由于河南联通转换成的5元组，ip就是存的实际ip，没有转成16进制
				//用的cname作为区分
				String cip = null;
				String dip = null;
				if ("1.1-1.1".equals(tmp[0])) {
				    cip = tmp[2];
				    dip = tmp[3];
				} else {
				    cip = long2IP(Long.valueOf(tmp[2], 16));
                    dip = long2IP(Long.valueOf(tmp[3], 16));
				}
				structDNS.setCip(cip);
				structDNS.setDip(dip);
				int visitCount = Integer.valueOf(tmp[4]);
				structDNS.setVisitCount(visitCount);
				structDNS.setDomain(tmp[1]);
				return structDNS;
			} catch (Exception e) {
				LOG.error("error dns data: \"" + text + "\"", e);
			}
		}
		return null;
	}

	/**
	 * 江苏DNS解析方法
	 * 
	 * @param text
	 * @return
	 */
	public static StructDNS parseJsDNSRecord(String text) {
		String[] tmp = text.split("\\|");
		if (tmp.length >= 5) {
			try {
				StructDNS structDNS = new StructDNS();
				String visitTime = tmp[6];
				structDNS.setVisitTime(visitTime);
				String cip = tmp[3];
				structDNS.setCip(cip);
				String dip = tmp[2];
				structDNS.setDip(dip);
				int visitCount = 1;
				structDNS.setVisitCount(visitCount);
				structDNS.setDomain(tmp[0]);
				return structDNS;
			} catch (Exception e) {
				LOG.error("error dns data: \"" + text + "\"", e);
			}
		}
		return null;
	}

	public static long ip2Long(String ip) {
		String[] ips = ip.split("[.]");
		long num = Long.parseLong(ips[0]) << 24;
		num += Long.parseLong(ips[1]) << 16;
		num += Long.parseLong(ips[2]) << 8;
		num += Long.parseLong(ips[3]);
		return num;
	}
	
	public static long ipToLong(String ip){
		int pos1 = ip.indexOf(".");
		int pos2 = ip.indexOf(".",pos1+1);
		int pos3 = ip.indexOf(".",pos2+1);
		long num = Long.parseLong(ip.substring(0,pos1)) << 24;
		num += Long.parseLong(ip.substring(pos1+1,pos2)) << 16;
		num += Long.parseLong(ip.substring(pos2+1,pos3)) << 8;		
		num += Long.parseLong(ip.substring(pos3+1));
		return num;
	}

	public static String long2IP(long longIp) {
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf((longIp >>> 24)));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x000000FF)));
		return sb.toString();
	}

	public static void main(String[] args) {
		StructDNS structDNS = parseDNSRecord(DnsStatConsts.ZJ_PROVINCE, "www.apple.com|www.isg-apple.com.akadns.net|183.207.229.137|112.4.1.36|1|1|2015-1-22 14:02:43|");
		System.out.println(structDNS.getTopDomain());
		System.out.println(structDNS.getCip());
		System.out.println(structDNS.getDip());
		System.out.println(structDNS.getDomain());
		System.out.println(structDNS.getVisitCount());
	}
}

